/**
 * @copyright SilverBiology
 * @author Michael Giddens
 * @website http://www.silverbiology.com
*/

Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial');

GBIF.GeoSpatial.CopyLink = Ext.extend(Ext.Button, {		
		iconCls: 'icon-link'
	,	initComponent: function(){		
			GBIF.GeoSpatial.CopyLink.superclass.initComponent.call(this);
			this.linkBox = new Ext.BoxComponent({
				html: '<div class="CopyLinkText" padding-top="10px">Paste link in Email or IM</div><div class="CopyLinkInput"><input value=" " size=80/></div>'
			});
		}						

  ,	onDestroy: function(){
			this.linkBox.destroy();
			if(this.menu) {
				this.menu.destroy();
			}
			GBIF.GeoSpatial.CopyLink.superclass.onDestroy.call(this);
    }

	,	onClick : function(){
			this.fireEvent('doupdateUrl',this);
			if(this.disabled){
				return;
			}
			this.menu.on(Ext.apply({}, this.menuListeners, {
				scope:this
			}));
			this.menu.show(this.el, "tl-bl?");
		}
	
	,	menuListeners : {
				show : function(){
					this.onFocus();
				}
			,	hide : function(){
					this.focus.defer(10, this);
					var ml = this.menuListeners;
					this.menu.un("show", ml.show,  this);
					this.menu.un("hide", ml.hide,  this);
				}
    }
  	  
	,	onRender: function(){
			GBIF.GeoSpatial.CopyLink.superclass.onRender.apply(this, arguments);
			this.menu = new Ext.menu.Menu({
					cls: 'CopyLink'
				,	items: this.linkBox
			});
			this.menu.render();

			this.inputUrl = Ext.select('input', this.linkBox.body).first();			
			this.inputUrl.on('focus', function() {
					this.dom.select();
			});

		}
		
	,	updateUrl: function( url ) {
		//	var txtWeight = document.getElementById("txtWeight");
		//	txtWeight.value=url
				this.inputUrl.set({value: url});
		}
});